#include "stdafx.h"
#include "RF620DeviceNet.h"
#include "include\RF620Device.h"
#include "include\RFDeviceDefs.h"

using namespace RFDeviceNet;
using namespace System::Runtime::InteropServices;

RF620DeviceNet^ RF620DeviceNet::CreateRF620Device()
{
	RFDevice::RF620Device* device = new RFDevice::RF620Device();
	RF620DeviceNet^ dev = gcnew RF620DeviceNet(device);
	return dev;
}

RF620DeviceNet^ RF620DeviceNet::CreateRF620Device(_UDP_DEVINFOBLOCK_PC_Net^ devBlock)
{
	RFDevice::_UDP_DEVINFOBLOCK_PC* db = new RFDevice::_UDP_DEVINFOBLOCK_PC();
	devBlock->GetDevBlock(db);
	RFDevice::RF620Device* device = new RFDevice::RF620Device(db);
	RF620DeviceNet^ dev = gcnew RF620DeviceNet(device);
	return dev;
}

RF620DeviceNet::RF620DeviceNet()
{
	_rf620Device = new RF620Device();	
}

RF620DeviceNet::RF620DeviceNet(RF620Device* device) : RFLanDeviceNet(device)
{
	_rf620Device = device;
}

RF620DeviceNet::~RF620DeviceNet()
{
	_rf620Device->~RF620Device();
	_rf620Device = NULL;
}

bool RF620DeviceNet::Connect(){
	bool ret = _rf620Device->Connect();	
	return ret;
}


bool RF620DeviceNet::Connect(_UDP_DEVINFOBLOCK_PC_Net^ devBlock )
{
	_UDP_DEVINFOBLOCK_PC* db = new _UDP_DEVINFOBLOCK_PC();
	devBlock->GetDevBlock(db);
	bool ret = _rf620Device->Connect(db);
	return ret;
}

bool RF620DeviceNet::Disconnect(){
	return _rf620Device->Disconnect();
}

bool RF620DeviceNet::GetResult(array<byte>^% buffer){
	//return _rf620Device->GetResult((void*)buffer);
	char* p = new char[ProfileBufferSize];
	bool ret = _rf620Device->GetResult(p);
	buffer = gcnew array<byte>(ProfileBufferSize);
	for (int i = 0; i < ProfileBufferSize; i ++)
	{
		buffer[i] = p[i];
	}
	return ret;
}

bool RF620DeviceNet::GetImage(array<byte>^% buffer){
	char* p = new char[ImageBufferSize];
	bool ret = _rf620Device->GetImage(p);
	buffer = gcnew array<byte>(ImageBufferSize);
	for (int i = 0; i < ImageBufferSize; i ++)
	{
		buffer[i] = p[i];
	}
	return ret;
}

short RF620DeviceNet::GetResultInclinationAngle()
{
	return _rf620Device->GetResultInclinationAngle();
}

bool RF620DeviceNet::ReadParams()
{
	return _rf620Device->ReadParams();
}

bool RF620DeviceNet::WriteParams()
{
	return _rf620Device->WriteParams();
}

bool RF620DeviceNet::FlushParams(bool bDefault)
{
	return _rf620Device->FlushParams(bDefault);
}

bool RF620DeviceNet::UDPConnect (USHORT UDPPort, String^ localIPAddress)
{
	const char* str = (const char*)(void*)Marshal::StringToHGlobalAnsi(localIPAddress);
	bool ret =  _rf620Device->UDPConnect(UDPPort, str);
	return ret;
}

bool RF620DeviceNet::UDPDisconnect()
{
	return _rf620Device->UDPDisconnect();
}

bool RF620DeviceNet::UDPGetResult(array<byte>^% buffer)
{
	char* p = new char[ProfileBufferSize];
	bool ret = _rf620Device->UDPGetResult(p);
	buffer = gcnew array<byte>(ProfileBufferSize);
	for (int i = 0; i < ProfileBufferSize; i ++)
	{
		buffer[i] = p[i];
	}
	return ret;
}

bool RF620DeviceNet::PingTest()
{
	return _rf620Device->PingTest();
}

UInt16 RF620DeviceNet::UDPPacketCounter(IntPtr buffer)
{
	return RF620Device::UDPPacketCounter((void*) buffer);
}

//NotImplemented
/*
USHORT RF620DeviceNet::ConvertResultToPoints(array<float>^ resultBuffer, array<DWORD>^% pointsBuffer, USHORT% count)
{
	const int len = 2*1024;
	float* inMas = new float[2*1024+2];
	DWORD* outMas = new DWORD[len];
	USHORT* pCount = new USHORT;
	USHORT res = 0;//_rf620Device->ConvertResultToPoints(inMas, outMas, pCount);
	count = *pCount;
	pointsBuffer = gcnew array<DWORD>(len);
	for(int i = 0; i < len; i++)
	{		
		pointsBuffer[i] = outMas[i];
	}
	return res;
}*/

//NotImplemented
/*
USHORT RF620DeviceNet::GetNormalizedResult(array<DWORD>^% pointsBuffer, USHORT% count)
{
	const int len = 2*1024 + 2;
	DWORD* poutMas = new DWORD[len];
	USHORT* pCount = new USHORT;
	USHORT res = 0;//_rf620Device->GetNormalizedResult(poutMas, pCount);	
	count = *pCount;
	pointsBuffer = gcnew array<DWORD>(len);
	for(int i = 0; i < len; i++)
	{
		pointsBuffer[i] = poutMas[i];
	}
	return res;
}
*/

//NotImplemented
/*
USHORT RF620DeviceNet::UDPGetNormalizedResult(array<DWORD>^% pointsBuffer, USHORT% count)
{
	const int len = 2*1024 + 2;
	DWORD* poutMas = new DWORD[len];
	USHORT* pCount = new USHORT;
	USHORT res = 0;//_rf620Device->UDPGetNormalizedResult(poutMas, pCount);
	count = *pCount;
	pointsBuffer = gcnew array<DWORD>(len);
	for(int i = 0; i < len; i++)
	{
		pointsBuffer[i] = poutMas[i];
	}
	return res;
}
*/

//NotImplemented
/*
USHORT RF620DeviceNet::ConvertResultToPoints(array<USHORT>^ resultBuffer, array<float>^% pointsBuffer, USHORT% count)
{
	const int len = 2*1024 + 2;
	USHORT* pinMas = new USHORT[len];
	for (int i = 0; i < resultBuffer->Length; i ++)
	{
		pinMas[i] = resultBuffer[i];
	}
	float* poutMas = new float[2*1024];
	USHORT* pCount = new USHORT;
	USHORT res = 0;//_rf620Device->ConvertResultToPoints(pinMas, poutMas, pCount);
	count = *pCount;
	pointsBuffer = gcnew array<float>(len);
	for(int i = 0; i < len; i++)
	{
		pointsBuffer[i] = poutMas[i];
	}
	return res;
}
*/

//NotImplemented
/*
USHORT RF620DeviceNet::GetNormalizedResult(array<float>^% pointsBuffer, USHORT% count)
{
	const int len = 2*1024;
	float* outMas = new float[len];
	USHORT* pCount = new USHORT;
	USHORT res = 0;//_rf620Device->GetNormalizedResult(outMas, pCount);
	count = *pCount;
	pointsBuffer = gcnew array<float>(len);
	for(int i = 0; i < len; i++)
	{		
		pointsBuffer[i] = outMas[i];
	}
	return res;
}
*/

//NotImplemented
/*
USHORT RF620DeviceNet::UDPGetNormalizedResult(array<float>^% pointsBuffer, USHORT% count)
{
	const int len = 2*1024;
	float* outMas = new float[len];
	USHORT* pCount = new USHORT;
	USHORT res = 0;//_rf620Device->UDPGetNormalizedResult(outMas, pCount);
	count = *pCount;
	pointsBuffer = gcnew array<float>(len);
	for(int i = 0; i < len; i++)
	{		
		pointsBuffer[i] = outMas[i];
	}
	return res;
}
*/

DWORD RF620DeviceNet::GetSerialNumber()
{
	return _rf620Device->GetSerialNumber();
}

bool RF620DeviceNet::GetCapsExternalResultSync()
{
	return _rf620Device->GetCapsExternalResultSync();
}

bool RF620DeviceNet::GetCapsExternalMeasurementSync()
{
	return _rf620Device->GetCapsExternalMeasurementSync();
}

bool RF620DeviceNet::GetCapsAnalogueOutputByCurrent()
{
	return _rf620Device->GetCapsAnalogueOutputByCurrent();
}

bool RF620DeviceNet::GetCapsAnalogueOutputByVoltage()
{
	return _rf620Device->GetCapsAnalogueOutputByVoltage();
}

bool RF620DeviceNet::GetCapsLaserModulation()
{
	return _rf620Device->GetCapsLaserModulation();
}

WORD RF620DeviceNet::GetBaseDistance()
{
	return _rf620Device->GetBaseDistance();
}

WORD RF620DeviceNet::GetMeasurementRangeZ()
{
	return _rf620Device->GetMeasurementRangeZ();
}

WORD RF620DeviceNet::GetMeasurementRangeXSMR()
{
	return _rf620Device->GetMeasurementRangeXSMR();
}

WORD RF620DeviceNet::GetMeasurementRangeXEMR()
{
	return _rf620Device->GetMeasurementRangeXEMR();
}

DWORD RF620DeviceNet::GetFirmwareVersion()
{
	return _rf620Device->GetFirmwareVersion();
}

BYTE RF620DeviceNet::GetLaserLevel()
{
	return _rf620Device->GetLaserLevel();
}

WORD RF620DeviceNet::GetExposureTime()
{
	return _rf620Device->GetExposureTime();
}

WORD RF620DeviceNet::GetSignalProcessingTime()
{
	return _rf620Device->GetSignalProcessingTime();
}

BYTE RF620DeviceNet::GetLeftBoundary()
{
	return _rf620Device->GetLeftBoundary();
}

BYTE RF620DeviceNet::GetWindowWidth()
{
	return _rf620Device->GetWindowWidth();
}

BYTE RF620DeviceNet::GetUpperBoundary()
{
	return _rf620Device->GetUpperBoundary();
}

BYTE RF620DeviceNet::GetWindowHeight()
{
	return _rf620Device->GetWindowHeight();
}

bool RF620DeviceNet::GetExternalResultSyncEnabled()
{
	return _rf620Device->GetExternalResultSyncEnabled();
}

bool RF620DeviceNet::GetExternalResultSyncEdge()
{
	return _rf620Device->GetExternalResultSyncEdge();
}

bool RF620DeviceNet::GetExternalMeasurementSyncEnabled()
{
	return _rf620Device->GetExternalMeasurementSyncEnabled();
}

bool RF620DeviceNet::GetExternalMeasurementSyncEdge()
{
	return _rf620Device->GetExternalMeasurementSyncEdge();
}

bool RF620DeviceNet::GetAnalogueOutputSyncEnabled()
{
	return _rf620Device->GetAnalogueOutputSyncEnabled();
}

bool RF620DeviceNet::GetAnalogueOutputSyncEdge()
{
	return _rf620Device->GetAnalogueOutputSyncEdge();
}

bool RF620DeviceNet::GetAnalogueOutputTTL_OUT0Level()
{
	return _rf620Device->GetAnalogueOutputTTL_OUT0Level();
}

BYTE RF620DeviceNet::GetExternalSignalDivisor()
{
	return _rf620Device->GetExternalSignalDivisor();
}

BYTE RF620DeviceNet::GetMeasurementDelay()
{
	return _rf620Device->GetMeasurementDelay();
}

BYTE RF620DeviceNet::GetAnalogueOutputNumberOfPoints()
{
	return _rf620Device->GetAnalogueOutputNumberOfPoints();
}

BYTE RF620DeviceNet::GetAnalogueOutputPointHoldupTime()
{
	return _rf620Device->GetAnalogueOutputPointHoldupTime();
}

array<Byte>^ RF620DeviceNet::GetDeviceIPAddress()
{
	const byte * mas = _rf620Device->GetDeviceIPAddress();
	array<Byte>^ retMas = gcnew array<Byte>(4);
	retMas[0] = mas[0];
	retMas[1] = mas[1];
	retMas[2] = mas[2];
	retMas[3] = mas[3];
	return retMas;
}

array<Byte>^ RF620DeviceNet::GetHostIPAddress()
{
	const byte * mas = _rf620Device->GetHostIPAddress();
	array<Byte>^ retMas = gcnew array<Byte>(4);
	retMas[0] = mas[0];
	retMas[1] = mas[1];
	retMas[2] = mas[2];
	retMas[3] = mas[3];
	return retMas;
}

array<Byte>^ RF620DeviceNet::GetNetworkMask()
{
	const byte * mas = _rf620Device->GetNetworkMask();
	array<Byte>^ retMas = gcnew array<Byte>(4);
	retMas[0] = mas[0];
	retMas[1] = mas[1];
	retMas[2] = mas[2];
	retMas[3] = mas[3];
	return retMas;
}

String^ RF620DeviceNet::GetHumanReadableDeviceIPAddress()
{
	char * c = _rf620Device->GetHumanReadableDeviceIPAddress();
	String^ str = gcnew String(c);
	return str;
}

String^ RF620DeviceNet::GetHumanReadableHostIPAddress()
{
	char * c = _rf620Device->GetHumanReadableHostIPAddress();
	String^ str = gcnew String(c);
	return str;
}

String^ RF620DeviceNet::GetHumanReadableNetworkMask()
{
	char * c = _rf620Device->GetHumanReadableNetworkMask();
	String^ str = gcnew String(c);
	return str;
}

WORD RF620DeviceNet::GetHostPortNumber()
{
	return _rf620Device->GetHostPortNumber();
}

BYTE RF620DeviceNet::GetUDPFrequency()
{
	return _rf620Device->GetUDPFrequency();
}

short RF620DeviceNet::GetRotationAngle()
{
	return _rf620Device->GetRotationAngle();
}

bool RF620DeviceNet::SetLaserLevel(BYTE value)
{
	BYTE valueToSet = value;
	bool ret = _rf620Device->SetLaserLevel(valueToSet);
	return ret;
}

bool RF620DeviceNet::SetExposureTime(WORD value)
{
	return _rf620Device->SetExposureTime(value);
}

bool RF620DeviceNet::SetSignalProcessingTime(WORD value)
{
	return _rf620Device->SetSignalProcessingTime(value);
}

bool RF620DeviceNet::SetLeftBoundary(BYTE value)
{
	return _rf620Device->SetLeftBoundary(value);
}

bool RF620DeviceNet::SetWindowWidth(BYTE value)
{
	return _rf620Device->SetWindowWidth(value);
}

bool RF620DeviceNet::SetUpperBoundary(BYTE value)
{
	return _rf620Device->SetUpperBoundary(value);
}

bool RF620DeviceNet::SetWindowHeight(BYTE value)
{
	return _rf620Device->SetWindowHeight(value);
}

bool RF620DeviceNet::SetExternalResultSyncEnabled(bool value)
{
	return _rf620Device->SetExternalResultSyncEnabled(value);
}

bool RF620DeviceNet::SetExternalResultSyncEdge(bool value)
{
	return _rf620Device->SetExternalResultSyncEdge(value);
}

bool RF620DeviceNet::SetExternalMeasurementSyncEnabled(bool value)
{
	return _rf620Device->SetExternalMeasurementSyncEnabled(value);
}

bool RF620DeviceNet::SetExternalMeasurementSyncEdge(bool value)
{
	return _rf620Device->SetExternalMeasurementSyncEdge(value);
}

bool RF620DeviceNet::SetAnalogueOutputSyncEnabled(bool value)
{
	return _rf620Device->SetAnalogueOutputSyncEnabled(value);
}

bool RF620DeviceNet::SetAnalogueOutputSyncEdge(bool value)
{
	return _rf620Device->SetAnalogueOutputSyncEdge(value);
}

bool RF620DeviceNet::SetAnalogueOutputTTL_OUT0Level(bool value)
{
	return _rf620Device->SetAnalogueOutputTTL_OUT0Level(value);
}

bool RF620DeviceNet::SetExternalSignalDivisor(BYTE value)
{
	return _rf620Device->SetExternalSignalDivisor(value);
}

bool RF620DeviceNet::SetMeasurementDelay(BYTE value)
{
	return _rf620Device->SetMeasurementDelay(value);
}

bool RF620DeviceNet::SetAnalogueOutputNumberOfPoints(BYTE value)
{
	return _rf620Device->SetAnalogueOutputNumberOfPoints(value);
}

bool RF620DeviceNet::SetAnalogueOutputPointHoldupTime(BYTE value)
{
	return _rf620Device->SetAnalogueOutputPointHoldupTime(value);
}

bool RF620DeviceNet::SetDeviceIPAddress(array<Byte>^ value)
{
	BYTE bValue[4];
	bValue[0] = value[0];
	bValue[1] = value[1];
	bValue[2] = value[2];
	bValue[3] = value[3];
	return _rf620Device->SetDeviceIPAddress(bValue);
}

bool RF620DeviceNet::SetHostIPAddress(array<Byte>^ value)
{
	BYTE bValue[4];
	bValue[0] = value[0];
	bValue[1] = value[1];
	bValue[2] = value[2];
	bValue[3] = value[3];
	return _rf620Device->SetHostIPAddress(bValue);
}

bool RF620DeviceNet::SetNetworkMask(array<Byte>^ value)
{
	BYTE bValue[4];
	bValue[0] = value[0];
	bValue[1] = value[1];
	bValue[2] = value[2];
	bValue[3] = value[3];
	return _rf620Device->SetNetworkMask(bValue);
}

bool RF620DeviceNet::SetHostPortNumber(WORD value)
{
	return _rf620Device->SetHostPortNumber(value);
}

bool RF620DeviceNet::SetUDPFrequency(BYTE value)
{
	return _rf620Device->SetUDPFrequency(value);
}

bool RF620DeviceNet::SetRotationAngle(short value)
{
	return _rf620Device->SetRotationAngle(value);
}

_UDP_DEVINFOBLOCK_PC_Net^ RF620DeviceNet::GetDevBlock()
{
	RFDevice::_UDP_DEVINFOBLOCK_PC* dev = _rf620Device->GetDevBlock();
	_UDP_DEVINFOBLOCK_PC_Net^ ndev = gcnew _UDP_DEVINFOBLOCK_PC_Net(dev);
	return ndev;
}

void RF620DeviceNet::CloneParameters(RF620DeviceNet^% dev)
{
	dev = gcnew RF620DeviceNet(_rf620Device);
}